package com.ruoyi.web.wapper;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.service.IUploadFileService;
import com.ruoyi.common.wapper.BaseControllerWrapper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

public class CountInfoWapper extends BaseControllerWrapper {

    public CountInfoWapper(Map<String, Object> single) {
        super(single);
    }

    public CountInfoWapper(List<Map<String, Object>> multi) {
        super(multi);
    }

    @Override
    public void wrapTheMap(Map<String, Object> map) {

        try {

            Integer uvCount = (Integer) map.get("uvCount");
            BigDecimal uvCost = (BigDecimal) map.get("uvCost");

            BigDecimal channelRate = (BigDecimal) map.get("channelRate");

            // 注册数量
            Integer registerCount = (Integer) map.get("registerCount");
            Integer registerCountReal = (Integer) map.get("registerCountReal");// 真实注册数据
            BigDecimal channelPrice = (BigDecimal) map.get("channelPrice");
            BigDecimal registerCost = (BigDecimal) map.get("registerCost");// 真实注册成本
            map.put("registerRate", countRate(new BigDecimal(registerCount), new BigDecimal(uvCount)));
            map.put("registerRateReal", countRate(new BigDecimal(registerCountReal), new BigDecimal(uvCount)));

            BigDecimal registerCostOff = BigDecimal.ZERO;
            BigDecimal registerCountOff = BigDecimal.ZERO;
            if (ObjectUtil.isNotEmpty(channelRate)){
                // TODO: 2022/12/6
                registerCountOff = channelRate.multiply(new BigDecimal(registerCountReal)).setScale(0,RoundingMode.DOWN);// 真实注册率
                registerCostOff = registerCountOff.multiply(channelPrice);// 真实注册成本扣

                map.put("registerCountOff", registerCountOff);
                map.put("registerCostOff", registerCostOff);
            }

            Integer authCount = (Integer) map.get("authCount");
            map.put("authRate", countRate(new BigDecimal(authCount), new BigDecimal(registerCountReal)));

            Integer downloadCount = (Integer) map.get("downloadCount");
            map.put("downloadRate", countRate(new BigDecimal(downloadCount), new BigDecimal(registerCountReal)));// 注册
            map.put("downloadRateAuth", countRate(new BigDecimal(downloadCount), new BigDecimal(authCount)));// 认证

            Integer uvProdList = (Integer) map.get("uvProdList");
            Integer proUvCount = (Integer) map.get("proUvCount");
            map.put("uvTransRate", countRate(new BigDecimal(proUvCount), new BigDecimal(uvProdList)));

            map.put("uvProduceRate", countRate(new BigDecimal(proUvCount), new BigDecimal(uvCount)));

            BigDecimal proUvProfit = (BigDecimal) map.get("proUvProfit");
            BigDecimal proUnionProfit = (BigDecimal) map.get("proUnionProfit");

            BigDecimal cost = uvCost.add(registerCostOff);
            BigDecimal profit = proUvProfit.add(proUnionProfit);

            // uv平均收益
            map.put("uvAvgProfit", divide(profit, new BigDecimal(proUvCount)));
            // roi
            map.put("roiRate", countRate(profit, cost));

            BigDecimal spePushAmount = (BigDecimal) map.get("spePushAmount");
            BigDecimal formPushAmount = (BigDecimal) map.get("formPushAmount");

            BigDecimal pushAmount = NumberUtil.add(spePushAmount, formPushAmount);
            map.put("pushRoiRate", countRate(pushAmount, cost));

            Integer riskRequestNumber = (Integer) map.get("riskRequestNumber");
            Integer riskSuccessNumber = (Integer) map.get("riskSuccessNumber");
            map.put("riskSuccessRate", countRate(new BigDecimal(riskSuccessNumber), new BigDecimal(riskRequestNumber)));

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    private String divide(BigDecimal one, BigDecimal two){
        if (ObjectUtil.isEmpty(two) || two.compareTo(BigDecimal.ZERO) ==0){
            return "0";
        }
        if (ObjectUtil.isEmpty(one) || one.compareTo(BigDecimal.ZERO) ==0){
            return "0";
        }
        return one.divide(two, 2, RoundingMode.HALF_UP).toString();
    }

    private String countRate(BigDecimal one, BigDecimal two){

        if (ObjectUtil.isEmpty(two) || two.compareTo(BigDecimal.ZERO) ==0){
            return "0%";
        }
        if (ObjectUtil.isEmpty(one) || one.compareTo(BigDecimal.ZERO) ==0){
            return "0%";
        }
        return one.divide(two, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(2) + "%";
    }
}
